﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;

namespace Common.Tool.ControlTools
{
    public class ControlHelper
    {
        public static T FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            DependencyObject parentObject = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                parentObject = VisualTreeHelper.GetParent(child);
            });
            // 如果到达了树的顶端，返回null
            if (parentObject == null) return null;

            // 检查父级控件是否为所需类型
            T parent = parentObject as T;
            if (parent != null)
            {
                return parent;
            }
            else
            {
                // 递归调用以继续向上遍历
                return FindParent<T>(parentObject);
            }

        }
    }
}
